package com.lhd.ivos.user.service.impl;


import com.lhd.ivos.base.exception.ServiceException;
import com.lhd.ivos.base.response.StatusCode;
import com.lhd.ivos.user.mapper.UserMapper;
import com.lhd.ivos.user.pojo.dto.UserLoginParam;
import com.lhd.ivos.user.pojo.dto.UserQuery;
import com.lhd.ivos.user.pojo.dto.UserSaveParam;
import com.lhd.ivos.user.pojo.entity.User;
import com.lhd.ivos.user.pojo.vo.UserVO;
import com.lhd.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;
    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录，参数：{}",userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if(userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    /**
     * 保存用户信息
     * @param userSaveParam 用户保存参数，包含用户基本信息
     * @return 返回保存用户的自增ID
     * @throws ServiceException 如果用户已存在，则抛出异常
     */
    @Override
    public int save(UserSaveParam userSaveParam) {
        log.debug("保存用户信息，参数：{}",userSaveParam);

        User user = new User();
        // 从用户保存参数复制属性到用户对象
        BeanUtils.copyProperties(userSaveParam,user);
        if (user.getId() == null){
            if (userMapper.selectByUsername(user.getUsername())!=null){
                throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
            }
            //注意:员工数据不是员工自己注册的，所以需要预设一个初始密码
            user.setPassword("123456");
            user.setCreateTime(new Date());
            return userMapper.insert(user);
        }else{
            user.setUpdateTime(new Date());
            return userMapper.update(user);
        }

    }

    @Override
    public List<UserVO> selectUsers(UserQuery userQuery) {
        List<UserVO> list = userMapper.selectUsers(userQuery);
        return list;
    }

    @Override
    public void updatePassword(Long userId) {
        User user = new User();
        user.setId(userId);
        user.setPassword("admin");
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long userId, String status) {
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long userId) {
        userMapper.deleteUser(userId);
    }

    @Override
    public List<UserVO> selectAuditByParentId(Long parentId) {
        ArrayList<UserVO> userVOList = new ArrayList<>();
        //根据上级用户id将审批人查出,添加到list中
        UserVO auditUser1 = userMapper.selectAuditByParentId(parentId);
        userVOList.add(auditUser1);
        //如果查到有上级,且上级还有上级
        if (auditUser1 !=null && auditUser1.getParentId() !=null){
            //查询上上级,添加到list中
            UserVO auditUser2 = userMapper.selectAuditByParentId(auditUser1.getParentId());
            userVOList.add(auditUser2);
        }
        return userVOList;
    }

}
